package com.wgts.wgts.mvp.model.imp;

import com.wgts.wgts.AppConstant;
import com.wgts.wgts.mvp.model.IStartPageModel;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.NetService;
import com.wgts.wgts.netmodle.TransformUtils;
import com.wgts.wgts.netmodle.resp.AdvertisingEntity;
import com.wgts.wgts.rxjava.MVPResultSubscriber;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.CountDownTime;
import com.wgts.wgts.tools.CountDownTimerListener;
import com.wgts.wgts.tools.SharedpreferencesHelper;

import rx.Observable;
import rx.Subscriber;

public class StartPageModel implements IStartPageModel {

    private SharedpreferencesHelper sharedpreferencesHelper;

    private AppInfor appInfor;

    private CountDownTime countDownTime;

    private CountDownTimerListener countDownTimerListener;

    private int timeSecond = 0;

    private NetService netService;


    public StartPageModel(SharedpreferencesHelper sharedpreferencesHelper, AppInfor appInfor, CountDownTime countDownTime, NetService netService) {
        this.sharedpreferencesHelper = sharedpreferencesHelper;
        this.appInfor = appInfor;
        this.countDownTime = countDownTime;
        this.netService = netService;
    }

    @Override
    public void jumpResult(MVPResultSubscriber<Boolean> subscriber) {
        Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                int versionCode = sharedpreferencesHelper.getInt(AppConstant.SHARED_VERSION);
                if(versionCode == appInfor.getAppVersionCode()){
                    subscriber.onNext(true);
                }else{
                    subscriber.onNext(false);
                }
            }
        }).subscribe(subscriber);
    }

    @Override
    public void jumpTime(final MVPResultSubscriber<String> subscriber) {


        countDownTimerListener = new CountDownTimerListener() {
            @Override
            public void countDownTimerListener() {
                timeSecond++;
                if(timeSecond == 3){
                    Observable.create(new Observable.OnSubscribe<String>() {
                        @Override
                        public void call(Subscriber<? super String> subscriber) {
                            subscriber.onNext("success");
                        }
                    }).subscribe(subscriber);
                }
            }
        };

        countDownTime.addCountDownTimerListener(countDownTimerListener);

    }

    @Override
    public void removeTime() {
        countDownTime.removeCountDownTimerListener(countDownTimerListener);
        countDownTimerListener = null;
    }

    @Override
    public void advertising(HttpResultSubscriber<AdvertisingEntity> subscriber) {
        netService.advertising(subscriber);
    }


    /**
     * 广告页
     * @param subscriber
     */
    @Override
    public void jumpTimeSecond(final MVPResultSubscriber<String> subscriber) {
        countDownTimerListener = new CountDownTimerListener() {
            @Override
            public void countDownTimerListener() {
                Observable.create(new Observable.OnSubscribe<String>() {
                    @Override
                    public void call(Subscriber<? super String> subscriber) {
                        subscriber.onNext("success");
                    }
                }).compose(TransformUtils.<String>defaultSchedulers()).subscribe(subscriber);
            }
        };

        countDownTime.addCountDownTimerListener(countDownTimerListener);
    }


}
